<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "400px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {},
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      var data1 = [20, 30, 40, 50];
      var data2 = [30, 30, 9, 60];
      var data3 = [20, 30, 20, 30];
      var data4 = [20, 30, 40, 50];
      var datacity = ["W1-W4", "W5-W8", "W9-W12"];
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        color: ["#3091F2", "#31C670", "#ED1B1B"],
        tooltip: {
          trigger: "axis",
          backgroundColor: "transparent", //标题背景色
          borderColor: "#fff", //边框颜色
          borderWidth: 0, //边框线宽
          padding: 5,
          borderRadius: 6,
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(255,255,255,0)", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255,255,255,1)", // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(255,255,255,0)", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
          },
          formatter: function (params) {
            return `<div style="background:#fff;padding: 20px;border-radius: 6px;
                        box-shadow: 0px 0px 12px 0px rgba(14,23,38,0.1);">
                        <div style="display: flex; align-items: center;
                        padding-bottom: 15px;
                        margin-bottom: 15px;
                        border-bottom: 1px solid #ebedf2;">
                            <i style="color: #1B84ED">预排产量(自工厂)</i>
                           
                        </div>
                        <div style="display: flex; align-items: center;
                        padding-bottom: 15px;
                        margin-bottom: 15px;
                        border-bottom: 1px solid #ebedf2;">
                        <i style="color: #8fa1c9">产能负荷率</i>
                        <p style="font-weight: 800; margin-left: 40px;color:#111;flex: 1;
                        text-align: right;">100%</p>
                        </div>
                        <div style="display: flex; align-items: center;
                        padding-bottom: 15px;
                        margin-bottom: 15px;
                        border-bottom: 1px solid #ebedf2;">
                        <i style="color: #8fa1c9">排产量</i>
                        <p style="font-weight: 800; margin-left: 40px;color:#111;flex: 1;
                        text-align: right;">
                        150000
                        </p>
                        </div>
                        <div style="display: flex; align-items: center;">
                        <i style="color: #8fa1c9">订单数</i>
                        <p style="font-weight: 800; margin-left: 40px;color:#111;flex: 1;
                        text-align: right;">
                        3
                        </p>
                        </div>
                    </div>
                    `;
          },
        },

        legend: {
          left: 0,
          top: 0,
          itemGap: 10, // 各个item之间的间隔，单位px，默认为10，
          itemWidth: 12, // 图例图形宽度
          itemHeight: 12,
          textStyle: {
            color: "#8FA1C9",
            fontWeight: "normal",
            fontSize: 16,
          },
        },
        grid: {
          left: "0%",
          right: "4%",
          bottom: "0",
          containLabel: true,
        },
        yAxis: [
          {
            type: "value",
            min: 0,
            max: 100,
            splitNumber: 5,
            splitLine: { show: false },
            axisLabel: {
              //坐标轴刻度标签的相关设置。
              show: true,
              textStyle: {
                color: "#A5B2CC",
              },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(131,101,101,0.2)",
              },
            },
            show: true,
          },
        ],
        xAxis: [
          {
            type: "category",
            axisTick: {
              show: false, // 是否显示坐标轴轴线
            },
            axisLabel: {
              color: "#A5B2CC",
            },
            splitLine: {
              show: false,
            },
            boundaryGap: true,
            axisLine: {
              //坐标轴轴线相关设置。
              show: true,
              inside: false,
              lineStyle: {
                color: "#EBEDF2",
              },
            },

            data: datacity,
          },
        ],
        series: [
          {
            name: "负荷正常",
            type: "bar",
            stack: "sum",
            barWidth: "16px",
            data: data1,
          },
          {
            name: "订单占比",
            type: "bar",
            barWidth: "16px",
            stack: "sum",
            data: data2,
          },
          {
            name: "超负荷",
            type: "bar",
            stack: "sum",
            barWidth: "16px",
            data: data3,
          },
        ],
      });
    },
  },
};
</script>

<style scoped lang="less"></style>
